import localCache from '@/utils/cache'
import { RouteRecordRaw } from 'vue-router'
import type { IBreadcrumb } from '@/base-ui/breadcrumb/types'

/**
 * 1: 根据用户菜单返回main子路由
 * @param userMenus 用户菜单
 * @returns main子路由数组
 */
export function mapMenusToRoutes(userMenus: any[]): RouteRecordRaw[] {
  //1:先加载所有路由
  const allRoutes: RouteRecordRaw[] = []
  const routeFiles = require.context('../router/main', true, /\.ts/)

  routeFiles.keys().forEach((key) => {
    const route = require('../router/main' + key.split('.')[1])
    allRoutes.push(route.default)
  })

  //2:根据菜单获取需要添加的routes
  // userMenus:
  // type === 1 -> children -> type === 1
  // type === 2 -> url -> route
  const routes: RouteRecordRaw[] = []

  const _filterRouterMenu = (userMenus: any[]) => {
    if (userMenus) {
      userMenus.forEach((item) => {
        if (item.type == 1) {
          _filterRouterMenu(item.children)
        } else if (item.type == 2) {
          const route = allRoutes.find((route) => route.path == item.url)
          if (route) {
            routes.push(route)
            if (routes.length == 1) {
              //保存第一个菜单
              localCache.saveFirstMenu(item)
            }
          }
        }
      })
    }
  }

  _filterRouterMenu(userMenus)

  return routes
}

/**
 * 2:根据path找到对应的菜单
 * @param userMenus  用户菜单列表
 * @param path  当前路径
 * @param breadcrumbList 面包屑数组
 * @returns path对应的菜单
 */
export function pathMapToMenu(
  userMenus: any[],
  path: string,
  breadcrumbList?: IBreadcrumb[]
): any {
  let activeMenu
  let parentMenu: any
  const _findMenu = (userMenus: any[]) => {
    if (userMenus) {
      userMenus.forEach((item) => {
        if (item.type == 1) {
          parentMenu = item
          _findMenu(item.children)
        } else if (item.type == 2) {
          if (path === item.url) {
            activeMenu = item
            if (parentMenu) {
              breadcrumbList?.push({
                name: parentMenu.name,
                path: parentMenu.url,
                id: parentMenu.id
              })
            }

            breadcrumbList?.push({
              name: item.name,
              path: item.url,
              id: item.id
            })
          }
        }
      })
    }
  }

  _findMenu(userMenus)
  return activeMenu
}

/**
 * 3:查找用户返回的菜单查找用户权限列表
 * @param userMenus  菜单
 * @returns
 */
export function mapMenusToPerMission(userMenus: any[] = []): any[] {
  const permissionList: any[] = []
  const _queryPermissonFun = (menus: any[] = []) => {
    menus.forEach((menu) => {
      if (menu.type == 3) {
        permissionList.push(menu.permission)
      } else {
        _queryPermissonFun(menu.children ?? [])
      }
    })
  }
  if (userMenus) {
    _queryPermissonFun(userMenus)
  }

  return permissionList
}
export function filterMenus(userMenus: any[] = [], type: number): any[] {
  const list: any[] = []
  const _queryMenu = (menus: any[] = []) => {
    menus.forEach((menu) => {
      if (menu.type == type) {
        list.push(menu)
      } else {
        _queryMenu(menu.children ?? [])
      }
    })
  }
  if (userMenus) {
    _queryMenu(userMenus)
  }

  return list
}
